﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace St0rm.IRCPro.Client {
    /// <summary>
    /// Contains user info from messages, nickname user and host
    /// </summary>
    public struct UserInfo {
        /// <summary>
        /// Users nickname
        /// </summary>
        public string nick;
        /// <summary>
        /// Users username or identd
        /// </summary>
        public string user;
        /// <summary>
        /// Users host
        /// </summary>
        public string host;
        /// <summary>
        /// Users hoststring nick!user@host
        /// </summary>
        public string HostString {
            get {
                return String.Format("{0}!{1}@{2}", nick, user, host);
            }
        }
    }
    /// <summary>
    /// Holds string utility
    /// </summary>
    public static class StringUtil {
        static System.Text.RegularExpressions.Regex rUserInfo = new System.Text.RegularExpressions.Regex(@"^([^!]+)!([^@]+)@(\S+)$");
        /// <summary>
        /// Parses UserInfo hut of a HostString
        /// </summary>
        /// <param name="hoststring">Host string to parse nick!user@host</param>
        /// <returns>UserInfo</returns>
        public static UserInfo ParseUserInfo(this string hoststring) {
            if (rUserInfo.IsMatch(hoststring)) {
                System.Text.RegularExpressions.Match match = rUserInfo.Match(hoststring);
                UserInfo info = new UserInfo();
                info.nick = match.Groups[1].Value;
                info.user = match.Groups[2].Value;
                info.host = match.Groups[3].Value;
                return info;
            }
            return new UserInfo();
        }
    }
}
